﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlinePayment
{
    /// <summary>
    /// 网银功能
    /// </summary>
    public class ChinaPayUtils
    {
        public static string priKeyPath = "";
        public static string pubKeyPath = "";
        public ChinaPayUtils()
        {
        }

        //2007签名
        public static string signData(string merId, string plain)
        {
            byte[] StrRes = Encoding.GetEncoding("utf-8").GetBytes(plain);
            plain = Convert.ToBase64String(StrRes);

            //NetPayClientClass a = new NetPayClientClass();
            NetPay a = new NetPay();
            //设置密钥文件地址
            a.buildKey(merId, 0, priKeyPath);

            // 对一段字符串的签名
            return a.Sign(plain);

        }

        //2004签名
        public static string sign(string MerId, string OrdId, string TransAmt, string CuryId, string TransDate, string TransType)
        {

            //NetPayClientClass a = new NetPayClientClass();
            NetPay a = new NetPay();
            //设置密钥文件地址
            a.buildKey(MerId, 0, priKeyPath);

            // 对一段字符串的签名
            return a.signOrder(MerId, OrdId, TransAmt, CuryId, TransDate, TransType);

        }




        //验签
        public static string checkData(string plain, string ChkValue)
        {
            byte[] StrRes = Encoding.GetEncoding("utf-8").GetBytes(plain);
            plain = Convert.ToBase64String(StrRes);

            NetPay a = new NetPay();
            //设置密钥文件地址
            a.buildKey("999999999999999", 0, pubKeyPath);

            // 对一段字符串的签名
            if (a.verifyAuthToken(plain, ChkValue))
            {
                return "0";
            }
            else
            {
                return "-118";
            }



        }


        //得到交易日期
        public static string getMerDate()
        {
            return DateTime.Now.ToString("yyyyMMdd");
        }

        //得到订单号16位
        public static string getMerSeqId()
        {
            return DateTime.Now.ToString("yyyyMMHHmmffffff");
        }

        //16位退款号
        public static string getMerRefId()
        {
            return DateTime.Now.ToString("yyyyMMHHmmffffff");
        }

        public static string getBase64(string str)
        {
            byte[] StrRes = Encoding.GetEncoding("utf-8").GetBytes(str);
            return Convert.ToBase64String(StrRes);
        }
 
    }
}
